package com.demo.server.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.demo.server.annotation.HandleLog;
import com.demo.server.constant.HandleLogChoice;
import com.demo.server.entity.User;
import com.demo.server.entity.req.IdReq;
import com.demo.server.entity.req.LoginReq;
import com.demo.server.entity.req.UserEditSelfReq;
import com.demo.server.entity.req.UserReq;
import com.demo.server.entity.req.UserSearchReq;
import com.demo.server.entity.resp.LoginResp;
import com.demo.server.service.UserService;
import com.demo.server.util.Result;
import com.demo.server.util.ResultData;
import com.demo.server.util.ResultPage;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@RestController
@RequestMapping("user")
@Tag(name= "用户接口")
public class UserController {
	
	@Autowired
	UserService userService;
	
	@HandleLog(value=HandleLogChoice.RECORD_SUC, type=HandleLogChoice.TYPE_LOGIN)
	@PostMapping("login")
    @Operation(summary = "登录")
	public ResultData<LoginResp> login(@RequestBody LoginReq req){
		log.info("login>>:{}",req);
		return userService.login(req);
	}
	
	@PostMapping("logout")
    @Operation(summary = "退出登录")
	@Parameter(name="token",description = "token",required = true,in = ParameterIn.HEADER)
	public Result logout(){
		log.info("logout>>");
		return userService.logout();
	}

	@PostMapping("search")
    @Operation(summary = "查询用户")
	@Parameter(name="token",description = "token",required = true,in = ParameterIn.HEADER)
	public ResultPage<User> search(@RequestBody UserSearchReq req){
		log.info("search>>:{}",req);
		return userService.search(req);
	}
	
	@PostMapping("detail")
    @Operation(summary = "用户详情")
	@Parameter(name="token",description = "token",required = true,in = ParameterIn.HEADER)
	public ResultData<User> detail(@RequestBody IdReq req){
		log.info("detail>>:{}",req);
		return userService.detail(req.getId());
	}
	
	@HandleLog
	@PostMapping("add")
    @Operation(summary = "添加用户")
	@Parameter(name="token",description = "token",required = true,in = ParameterIn.HEADER)
    public Result add(@RequestBody UserReq req){
		log.info("add>>:{}",req);
        return userService.add(req);
    }
	
	@HandleLog
	@PostMapping("edit")
    @Operation(summary = "修改用户")
	@Parameter(name="token",description = "token",required = true,in = ParameterIn.HEADER)
    public Result edit(@RequestBody UserReq req){
		log.info("edit>>:{}",req);
        return userService.edit(req);
    }
	
	@HandleLog
	@PostMapping("editSelf")
    @Operation(summary = "修改自身")
	@Parameter(name="token",description = "token",required = true,in = ParameterIn.HEADER)
    public Result editSelf(@RequestBody UserEditSelfReq req){
		log.info("editSelf>>:{}",req);
        return userService.editSelf(req);
    }
	
	@HandleLog
	@PostMapping("delete")
    @Operation(summary = "删除用户")
	@Parameter(name="token",description = "token",required = true,in = ParameterIn.HEADER)
    public Result delete(@RequestBody IdReq req){
    	log.info("delete>>:{}",req);
		return userService.delete(req.getId());
    }
}
